package com.duoketech.service.web.activity;
import com.duoketech.config.Constant;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.service.AbstractService;
import com.duoketech.model.activity.Activity;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.NotEqualCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

public class ActivityService extends AbstractService{
    public static ActivityService ME = new ActivityService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.NAME),"活动名称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.NAME),"活动名称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.DESC),"活动描述项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.DESC),"活动描述项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.PRICE),"活动价格项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.PRICE),"活动价格项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.START_TIME),"开始时间项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.START_TIME),"开始时间项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.END_TIME),"结束时间项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.END_TIME),"结束时间项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.CONTENT),"活动内容项为必填信息"));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.STATUS),"活动状态项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.STATUS),"活动状态项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.CONTACTOR),"联系人项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.CONTACTOR),"联系人项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Activity.MOBILE),"联系电话项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Activity.MOBILE),"联系电话项字符必须为1-50个字符",1,50));
        validator.excute();
    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return Activity.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {
        queryConstruct.and(new NotEqualCondition(Activity.STATUS,Activity.STATUS_DEL));
        queryConstruct.and(new EqualCondition(Activity.CREATOR,kv.getInt(Constant.MEMBER_ID_KEY)));
        String value = model.getStr(Activity.NAME);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(Activity.NAME,"%"+value+"%"));
        }
    }



    public List<ActivityOrder> queryActivityOrderBy(int activityId){

        String sql = "select * from " + ActivityOrder.dao.tableName() + " where obj_id=? and status=? order by id desc";
        List<ActivityOrder> activityOrder = ActivityOrder.dao.find(sql,activityId,ActivityOrder.STATUS_CONFIRM);

        for(ActivityOrder a:activityOrder){
            a.set( ActivityOrder.CARD_TYPE,a.cardType());
            a.set( ActivityOrder.STATUS,a.status());
        }
        return activityOrder;
    }
    
    public void doSaveWxActivity(Activity activity ,int memberId) throws ValidateException{
    	Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(activity.getStr(Activity.NAME),"活动名称项为必填信息"));
        validator.addRule(new StringLengthBetween(activity.getStr(Activity.NAME),"活动名称项字符必须为1-50个字符",1,50));
        validator.excute();
        activity.set(Activity.STATUS, Activity.STATUS_WX_SYNC);
    	activity.set(Activity.CREATOR, memberId);
    	activity.set(Activity.START_TIME, new Date());
    	activity.set(Activity.END_TIME, new Date());
    	activity.set(Activity.PRICE, PageUtil.yuanToFeng(10000));
    	activity.set(Activity.CREATE_TIME, new Date());
    	activity.save();
    }

}